﻿using BZhanHengjicvWPF.Common;
using BZhanHengjicvWPF.Common.Models;
using BZhanHengjicvWPF.Extensions;
using BZhanHengjicvWPF.Services;
using Microsoft.Win32;
using MyToDo.Dtos;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BZhanHengjicvWPF.ViewModels
{
    public class LoginViewModel : BindableBase, IDialogAware
    {
        private readonly ILoginService loginService;
        private readonly IEventAggregator eventAggregator;

        public LoginViewModel(ILoginService loginService,IEventAggregator eventAggregator)
        {
            this.loginService = loginService;
            this.eventAggregator = eventAggregator;
        }
        public string Title { get; set; }

        public event Action<IDialogResult> RequestClose;

        private string account; 
        private string passWord;


        /// <summary>
        /// 账号
        /// </summary>
        public string Account
        {
            get { return account; }
            set { account = value;RaisePropertyChanged(); }
        }
        private int selectIndex;
        /// <summary>
        /// 选择是注册页面或登录页面
        /// </summary>
        public int SelectIndex
        {
            get { return selectIndex; }
            set { selectIndex = value;  RaisePropertyChanged(); }
        }

        private RegisterDto userDto=new RegisterDto();

        public RegisterDto UserDto
        {
            get { return userDto; }
            set { userDto = value; RaisePropertyChanged(); }
        }


        /// <summary>
        /// 密码
        /// </summary>
        public string PassWord
        {
            get { return passWord; }
            set { passWord = value; RaisePropertyChanged(); }
        }


        public DelegateCommand<string> ExcuteCommand { get { return new DelegateCommand<string>(Excute); } }

        private void Excute(string arg)
        {
            switch (arg)
            {
                case "Login": Login(); break;
                case "Go": SelectIndex = 1; break;//跳转到注册界面
                case "Return": SelectIndex = 0; break;//返回登录界面
                case "Register": Register(); break;

            }
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private async void Register()
        {
            if (!string.IsNullOrWhiteSpace(UserDto.Account) || 
                !string.IsNullOrWhiteSpace(UserDto.UserName) || 
                !string.IsNullOrWhiteSpace(UserDto.Password) || 
                !string.IsNullOrWhiteSpace(UserDto.NewPassword))
            {
                if (UserDto.Password != UserDto.NewPassword)
                {
                    eventAggregator.SendMessage("两次密码不一致","Login");
                    return; }
                var result = await loginService.RegisgsterAsync(new UserDto()
                {
                    UseName = UserDto.UserName,
                    Account = UserDto.Account,
                    PassWord = UserDto.NewPassword
                });
                if (result.Status)
                {
                    //注册成功
                    eventAggregator.SendMessage("注册成功", "Login");
                    SelectIndex = 0;
                    return;
                }
                //注册失败
                eventAggregator.SendMessage("注册失败", "Login");
            }
        }

        private async void Login()
        {
            if (string.IsNullOrEmpty(Account) || string.IsNullOrEmpty(PassWord)) return;
            var result = await loginService.LoginAsync(new MyToDo.Dtos.UserDto() { Account = Account, PassWord = PassWord });
            if (result.Status)
            {
                AppSession.UserName = result.Result.UseName;
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
                eventAggregator.SendMessage("登录成功", "Login");//登录失败提示
                return;
            }
            eventAggregator.SendMessage(result.Message,"Login");//登录失败提示
        }
        /// <summary>
        /// 退出
        /// </summary>
        private void LogOut()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            LogOut();
        }


        public void OnDialogOpened(IDialogParameters parameters)
        {
            
        }
    }
}
